"""
__author__="yanwei"
__describe__="新建营销计划（第一步）"
__API__="/plan/step"
"""
import json
from lib.operate_excel import OperateExcel
from conf.readConfig import ReadConfig
from tool_class.get_token import GetToken
import os
import requests
import random
import time


class MarketingPlanStepone():
    def setUp(self):
        print("****开始执行****")

    def tearDown(self):
        print("--------执行结束--------")

    def marketing_plan_step_one(self):
        # 获取token
        token = GetToken().mobile()

        # 获取当前时间
        begin_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

        # 获取当天结束时间
        end_time = time.strftime('%Y-%m-%d 23:59:59', time.localtime(time.time()))

        host = ReadConfig().get_url("test_http_url")
        # 请求参数存放地址
        case_path = os.path.join(os.path.join(os.path.dirname(os.path.split(os.path.realpath(__file__))[0]), "data"),
                                 "接口用例.xls")
        # 请求数据存放标签页
        sheet_name = "新建营销计划（返回计划id）"
        operateexcle = OperateExcel()
        data = json.loads(operateexcle.excel_all_param(case_path, sheet_name)[0][6])
        request_param = {
            "info":
                {
                    "title": data["info"]["title"] + "_" + str(random.randint(100000, 999999999)),
                    "type": data["info"]["type"],
                    "reseller_id": data["info"]["reseller_id"],
                    "return_type": data["info"]["return_type"],
                    "reseller_name": data["info"]["reseller_name"],
                    "begin_time": str(begin_time),
                    "end_time": str(end_time)
                }
        }
        url = operateexcle.excel_all_param(case_path, sheet_name)[0][2]

        headers = {
            "Content-Type": json.loads(operateexcle.excel_all_param(case_path, sheet_name)[0][5])["Content-Type"],
            "authorization": token
        }

        resp = requests.post(url=host + url, headers=headers, json=request_param)
        result = resp.json()
        stepone = {
            "plan_id": result["data"]["id"],
            "plan_title": request_param["info"]["title"],
            "begin_time": request_param["info"]["begin_time"]
        }
        print("第一步：", result)

        return stepone


if __name__ == "__main__":
    MarketingPlanStepone().marketing_plan_step_one()
